﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace TAMSUP
{
    /// <summary>
    /// Interaction logic for cp.xaml
    /// </summary>
    /// 

    //kode 0 : contactperson
    //kode 1 : Rencana pembayaran

    public partial class cp : Window
    {
        private int state;
        private string idp;
        private string nk;
        public static SqlConnection Con = homeWindow.Con;
        public static SqlDataAdapter da;

        public cp(string ID_Penyewaan, string Nama_Klien, int kode)
        {
            this.idp = ID_Penyewaan;
            this.nk = Nama_Klien;
            this.state = kode;
            InitializeComponent();

            switch (kode) {
                case 0 : label1.Content = "Contact Person";
                    break;
                case 1: label1.Content = "Rencana Pembayaran";
                    this.Title = "TAMSUP :: Rencana Pembayaran";
                    break;
            }

            FillData();
        }

        public void FillData()
        {
            try
            {
                homeWindow.Con.Open();
                DataSet ds = new DataSet();
                if (state == 0)
                {
                    string query = @"SELECT c.ID_CP as [ID CP], c.Nama, c.Nomor_Telepon as [Nomor Telepon], c.email, c.Keterangan
                                    FROM Penyewaan AS a INNER JOIN Menghubungi AS b ON a.ID_Penyewaan = b.ID_Penyewaan
                                        JOIN ContactPerson AS c ON b.ID_CP = c.ID_CP
                                            WHERE a.ID_Penyewaan = '" + idp + "'";
                    da = new SqlDataAdapter(query, Con);
                    da.Fill(ds, "Daftar CP");
                    dataGrid1.ItemsSource = ds.Tables["Daftar CP"].DefaultView;
                    if (dataGrid1.Columns.Count > 0)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            dataGrid1.Columns[i].HeaderStyle = (Style)Resources["CenterAlignedColumnHeaderStyle"];
                        }
                    }
                }
                else if (state == 1)
                {
                    string query = @"SELECT b.ID_RP as [ID RP], b.Pembayaran_ke as [Pembayaran ke], Convert(varchar(10),b.Tanggal,103) as Tanggal, 'Rp ' + Convert(varchar(20),dbo.format_currency(b.Jumlah),1) as Jumlah, b.Keterangan
                                FROM Penyewaan AS a INNER JOIN RencanaPembayaran AS b ON a.ID_Penyewaan = b.ID_Penyewaan
                                    WHERE a.ID_Penyewaan = '" + idp + "'";
                    da = new SqlDataAdapter(query, Con);
                    da.Fill(ds, "Daftar Rencana Pembayaran");
                    dataGrid1.ItemsSource = ds.Tables["Daftar Rencana Pembayaran"].DefaultView;
                    if (dataGrid1.Columns.Count > 0)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            dataGrid1.Columns[i].HeaderStyle = (Style)Resources["CenterAlignedColumnHeaderStyle"];
                        }
                        dataGrid1.Columns[1].CellStyle = (Style)Resources["RightAlignedCellStyle"];
                        dataGrid1.Columns[3].CellStyle = (Style)Resources["RightAlignedCellStyle"];
                    }
                }
                homeWindow.Con.Close();
            }
            catch (SqlException sqle)
            {
                MessageBox.Show("Ada kesalahan pada koneksi ke server, silakan periksa jaringan.\n\n SQL error code : " + sqle.State, "Error : Connection Not Established");
            }
        }

        private void addCP(object sender, RoutedEventArgs e)
        {
            if (state == 0)
            {
                ContactPerson form = new ContactPerson(idp);
            }
            else // state == 1
            {
                addRencana form = new addRencana(idp);
            }
        }

        private void editCP(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.SelectedIndex >= 0)
            {
                if (state == 0)
                {
                    ContactPerson form = new ContactPerson(idp, ((DataRowView)(dataGrid1.SelectedItem)).Row.ItemArray.ElementAt(0).ToString());
                }
                else //state == 1
                {
                    addRencana form = new addRencana(idp, ((DataRowView)(dataGrid1.SelectedItem)).Row.ItemArray.ElementAt(0).ToString());
                }
            }
            else
            {
                MessageBox.Show("Anda harus memilih salah satu data", "Error: Null Selection");
            }
        }

        private void deleteCP(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.SelectedIndex >= 0)
            {
                homeWindow.top.dr = dataGrid1;
                if (state == 0)
                {
                    delete del = new delete(topWindow.CP, ((DataRowView)(dataGrid1.SelectedItem)).Row.ItemArray.ElementAt(0).ToString(), ((DataRowView)(dataGrid1.SelectedItem)).Row.ItemArray.ElementAt(1).ToString());
                    del.Visibility = System.Windows.Visibility.Visible;
                }
                else //state == 1
                {
                    delete del = new delete(topWindow.RENCANAPEMBAYARAN, ((DataRowView)(dataGrid1.SelectedItem)).Row.ItemArray.ElementAt(0).ToString());
                    del.Visibility = System.Windows.Visibility.Visible;
                }

            }
            else
            {
                MessageBox.Show("Anda harus memilih salah satu data", "Error: Null Selection");
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            label3.Content = idp;
            label4.Content = nk;
            FillData();
        }

        
    }
}
